package com.ruoyi.know.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.know.domain.Courses;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.know.domain.CourseChapters;
import com.ruoyi.know.service.ICourseChaptersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程章节Controller
 *
 * @author ruoyi
 * @date 2025-01-26
 */
@RestController
@RequestMapping("/know/courseChapters")
public class CourseChaptersController extends BaseController
{
    @Autowired
    private ICourseChaptersService courseChaptersService;

    /**
     * 查询课程章节列表
     */
    @PreAuthorize("@ss.hasPermi('know:courseChapters:list')")
    @GetMapping("/list")
    public TableDataInfo list(CourseChapters courseChapters,@RequestParam(required = false) String courseName)
    {
        startPage();
        if(courseName!=null&& !courseName.isEmpty()){
            Courses courses = new Courses();
            courses.setCourseName(courseName);
            courseChapters.setCourse(courses);
        }
        List<CourseChapters> list = courseChaptersService.selectCourseChaptersList(courseChapters);
        return getDataTable(list);
    }

    /**
     * 导出课程章节列表
     */
    @PreAuthorize("@ss.hasPermi('know:courseChapters:export')")
    @Log(title = "课程章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseChapters courseChapters)
    {
        List<CourseChapters> list = courseChaptersService.selectCourseChaptersList(courseChapters);
        ExcelUtil<CourseChapters> util = new ExcelUtil<CourseChapters>(CourseChapters.class);
        util.exportExcel(response, list, "课程章节数据");
    }

    /**
     * 获取课程章节详细信息
     */
    @PreAuthorize("@ss.hasPermi('know:courseChapters:query')")
    @GetMapping(value = "/{chapterId}")
    public AjaxResult getInfo(@PathVariable("chapterId") Long chapterId)
    {
        return success(courseChaptersService.selectCourseChaptersByChapterId(chapterId));
    }

    @GetMapping("getAll")
    public AjaxResult getAll(){
        List<CourseChapters> list = courseChaptersService.selectCourseChaptersList(null);
        return success(list);
    }

    /**
     * 新增课程章节
     */
    @PreAuthorize("@ss.hasPermi('know:courseChapters:add')")
    @Log(title = "课程章节", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CourseChapters courseChapters)
    {
        return toAjax(courseChaptersService.insertCourseChapters(courseChapters));
    }

    /**
     * 修改课程章节
     */
    @PreAuthorize("@ss.hasPermi('know:courseChapters:edit')")
    @Log(title = "课程章节", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CourseChapters courseChapters)
    {
        return toAjax(courseChaptersService.updateCourseChapters(courseChapters));
    }

    /**
     * 删除课程章节
     */
    @PreAuthorize("@ss.hasPermi('know:courseChapters:remove')")
    @Log(title = "课程章节", businessType = BusinessType.DELETE)
	@DeleteMapping("/{chapterIds}")
    public AjaxResult remove(@PathVariable Long[] chapterIds)
    {
        return toAjax(courseChaptersService.deleteCourseChaptersByChapterIds(chapterIds));
    }
}
